package TestPaperSystem.service.impl;

import TestPaperSystem.Dto.PageUtils_VO;
import TestPaperSystem.Dto.Page_Qu;
import TestPaperSystem.Dto.Qu_Vo;
import TestPaperSystem.entity.Answer;
import TestPaperSystem.entity.Qu;
import TestPaperSystem.entity.QuRepo;
import TestPaperSystem.entity.Repo;
import TestPaperSystem.mapper.QuMapper;
import TestPaperSystem.service.AnswerService;
import TestPaperSystem.service.QuRepoService;
import TestPaperSystem.service.QuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.security.auth.callback.Callback;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@Service
public class QuServiceImpl extends ServiceImpl<QuMapper, Qu> implements QuService {

    @Resource
    QuMapper quMapper;

    @Autowired
    AnswerService answerService;

    @Autowired
    QuRepoService quRepoService;

    @Override
    public Page getQuList(Page_Qu quDTO) {
        List<Qu> list = quMapper.getList(quDTO,(quDTO.getCurrentPage() - 1) * quDTO.getSize() , quDTO.getSize());
        Page page = new Page();
        page.setCurrent(quDTO.getCurrentPage());
        page.setRecords(list);
        Long counts = quMapper.getListByConditions(quDTO);
        page.setTotal(counts);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    //同时更新Qu answer QuRepo表
    public Boolean saveData(Qu_Vo quVo) {
        String id = String.valueOf(System.currentTimeMillis());
        Qu qu = new Qu();

        BeanUtils.copyProperties(quVo,qu);

        //新增
        if(quVo.getId() == null){

            qu.setId(id);
            //填充answer中的quId
            for(Answer e : quVo.getAnswerList()){
                e.setQuId(id);
            }


            return this.save(qu)  & quRepoService.saveData(quVo.getRepoIds(),qu) &
                    answerService.saveBatch(quVo.getAnswerList());
        }else{
            //修改
            return this.update(qu,new UpdateWrapper<Qu>().lambda().eq(Qu::getId,qu.getId())) &
                    quRepoService.saveData(quVo.getRepoIds(),qu) &
                    answerService.updateBatchById(quVo.getAnswerList());
        }
    }

    @Override
    public Qu_Vo Details(PageUtils_VO pageVo) {

        Qu_Vo qu_vo = new Qu_Vo();

        Qu qu = this.getById(pageVo.getId());

        BeanUtils.copyProperties(qu,qu_vo);

        List<Answer> list_answer = answerService.list(new QueryWrapper<Answer>().lambda().eq(Answer::getQuId,pageVo.getId()));
        qu_vo.setAnswerList(list_answer);
        List<String> list_quRepo = new ArrayList<>();
        for(QuRepo e:quRepoService.list(new QueryWrapper<QuRepo>().lambda()
                .eq(QuRepo::getQuId,pageVo.getId())
                .select(QuRepo::getRepoId)))
            list_quRepo.add(e.getRepoId());

        qu_vo.setRepoIds(list_quRepo);
        return qu_vo;
    }


    @Override
    public List<Qu> getList(int size,List<String> quIds , int quType , String repoId ){
        QueryWrapper<Qu> quQueryWrapper = new QueryWrapper<>();
        List<String> quListFromRepo = quRepoService.getQuListFromRepo(repoId, quIds, quType, size);
        if(quListFromRepo.isEmpty()){
            return null;
        }else{
            quQueryWrapper.lambda().in(Qu::getId , quListFromRepo);
        }


        return quMapper.selectList(quQueryWrapper);
    }
}
